package com.cristina.attention.interceptor;

import com.cristina.attention.utils.AttentionCommon;
import com.cristina.attention.utils.StringUtils;
import org.springframework.util.Base64Utils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author by superman
 */
public class ServerProtectInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(AttentionCommon.HEADER_TOKEN_GATEWAY);
        String gatewayToken = new String(Base64Utils.encode(AttentionCommon.HEADER_VALUE_GATEWAY.getBytes()));
        if (StringUtils.equals(gatewayToken, token)) {
            return AttentionCommon.TRUE;
        }
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("非法访问");
        return false;
    }
}
